//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.ComponentModel;
using DotNetNuke.Common.Utilities;
using System.Web;
namespace DotNetNuke.Common
{
	public partial class Global : System.Web.HttpApplication
	{
		private void Application_Start(object Sender, EventArgs E)
		{
			if (String.IsNullOrEmpty(Config.GetSetting("ServerName"))) {
				Globals.ServerName = Server.MachineName;
			} else {
				Globals.ServerName = Config.GetSetting("ServerName");
			}
			ComponentFactory.Container = new SimpleContainer();
			ComponentFactory.InstallComponents(new ProviderInstaller("data", typeof(DotNetNuke.Data.DataProvider)));
			ComponentFactory.InstallComponents(new ProviderInstaller("caching", typeof(Services.Cache.CachingProvider)));
			ComponentFactory.InstallComponents(new ProviderInstaller("logging", typeof(Services.Log.EventLog.LoggingProvider)));
			ComponentFactory.InstallComponents(new ProviderInstaller("scheduling", typeof(Services.Scheduling.SchedulingProvider)));
			ComponentFactory.InstallComponents(new ProviderInstaller("searchIndex", typeof(Services.Search.IndexingProvider)));
			ComponentFactory.InstallComponents(new ProviderInstaller("searchDataStore", typeof(Services.Search.SearchDataStoreProvider)));
			ComponentFactory.InstallComponents(new ProviderInstaller("friendlyUrl", typeof(Services.Url.FriendlyUrl.FriendlyUrlProvider)));
			ComponentFactory.InstallComponents(new ProviderInstaller("members", typeof(DotNetNuke.Security.Membership.MembershipProvider)));
			ComponentFactory.InstallComponents(new ProviderInstaller("roles", typeof(DotNetNuke.Security.Roles.RoleProvider)));
			ComponentFactory.InstallComponents(new ProviderInstaller("profiles", typeof(DotNetNuke.Security.Profile.ProfileProvider)));
			ComponentFactory.InstallComponents(new ProviderInstaller("permissions", typeof(DotNetNuke.Security.Permissions.PermissionProvider)));
			ComponentFactory.InstallComponents(new ProviderInstaller("outputCaching", typeof(DotNetNuke.Services.OutputCache.OutputCachingProvider)));
			ComponentFactory.InstallComponents(new ProviderInstaller("moduleCaching", typeof(DotNetNuke.Services.ModuleCache.ModuleCachingProvider)));
            ComponentFactory.InstallComponents(new ProviderInstaller("sitemap", typeof(DotNetNuke.Services.Sitemap.SitemapProvider)));
			DotNetNuke.Security.Permissions.PermissionProvider provider = DotNetNuke.ComponentModel.ComponentFactory.GetComponent<DotNetNuke.Security.Permissions.PermissionProvider>();
			if (provider == null) {
				ComponentFactory.RegisterComponentInstance<DotNetNuke.Security.Permissions.PermissionProvider>(new DotNetNuke.Security.Permissions.PermissionProvider());
			}
			ComponentFactory.InstallComponents(new ProviderInstaller("htmlEditor", typeof(Modules.HTMLEditorProvider.HtmlEditorProvider), ComponentLifeStyleType.Transient));
			ComponentFactory.InstallComponents(new ProviderInstaller("navigationControl", typeof(Modules.NavigationProvider.NavigationProvider), ComponentLifeStyleType.Transient));
		}
		private void Application_End(object Sender, EventArgs E)
		{
			Initialize.LogEnd();
			Initialize.StopScheduler();
		}
        private void Application_BeginRequest(object sender, EventArgs e)
		{
			HttpApplication app = (HttpApplication)sender;
			HttpRequest Request = app.Request;
			if (Request.Url.LocalPath.ToLower().EndsWith("scriptresource.axd") || Request.Url.LocalPath.ToLower().EndsWith("webresource.axd") || Request.Url.LocalPath.ToLower().EndsWith("gif") || Request.Url.LocalPath.ToLower().EndsWith("jpg") || Request.Url.LocalPath.ToLower().EndsWith("css") || Request.Url.LocalPath.ToLower().EndsWith("js")) {
				return;
			}
			Initialize.Init(app);
			Initialize.RunSchedule(Request);
		}
	}
}
